/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.util;

/**
 * 处理'32位无符号long'的转换.
 * 
 * @version 2014年3月8日下午1:37:30
 * @author Storm
 * @author jrwz
 */
public final class ULong32 {
    public final static long MAX_UNSIGNEDLONG_VALUE = 2147483647L;

    /**
     * 8位char数组转换为32位无符号long.
     * 
     * @param buff
     *            - 要转换的char数组
     * @return 无符号long
     */
    public static long fromArray(final char[] buff) {
        return fromLong64(((buff[3] & 0xFF) << 24) | ((buff[2] & 0xFF) << 16) | ((buff[1] & 0xFF) << 8) | (buff[0] & 0xFF));
    }

    /**
     * 8位byte数组转换为32位无符号long.
     * 
     * @param buff
     *            - 要转换的byte数组
     * @return 无符号long
     */
    public static long fromArray(final byte[] buff) {
        return fromLong64(((buff[3] & 0xFF) << 24) | ((buff[2] & 0xFF) << 16) | ((buff[1] & 0xFF) << 8) | (buff[0] & 0xFF));
    }

    /**
     * 64位java标准long转换为32位无符号long.
     * 
     * @param l
     *            - 要转换的long
     * @return 无符号long
     */
    public static long fromLong64(final long l) {
        return (((l << 32) >>> 32) & 0xFFFFFFFF);
    }

    /**
     * 32位java标准int转换为32位无符号long.
     * 
     * @param i
     *            - 要转换的int
     * @return 无符号long
     */
    public static long fromInt32(final int i) {
        return ((((long) i << 32) >>> 32) & 0xFFFFFFFF);
    }

    /**
     * 将2个32位(unsigned/signed)long值'相加'.
     * 
     * @param l1
     *            - 加
     * @param l2
     *            - 待加
     * @return 无符号long
     */
    public static long add(final long l1, final long l2) {
        return fromInt32((int) l1 + (int) l2);
    }

    /**
     * 将2个32位(unsigned/signed)long值'相减'.
     * 
     * @param l1
     *            - 减
     * @param l2
     *            - 待减
     * @return 无符号long
     */
    public static long sub(final long l1, final long l2) {
        return fromInt32((int) l1 - (int) l2);
    }
}
